#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <math.h>

using namespace std;

void main()
{
	ofstream os;

	os.open("Input.txt");

	int dim; 
	int width, height, depth;
	double max_v, _dt;

	cout << "Count of molecules: ";
	cin >> dim;
	cout << "Width of box: ";
	cin >> width;
	cout << "Height of box: ";
	cin >> height;
	cout << "Depth of box: ";
	cin >> depth;
	cout << "Maximum V: ";
	cin >> max_v;

	double *vx = new double[dim];
	double *vy = new double[dim];
	double *vz = new double[dim];

	double *x = new double[dim];
	double *y = new double[dim];
	double *z = new double[dim];

	srand( 666 );

	int cols;
	int rows;
	int dps;

	cols = rows = pow(double(dim),1./2.);
	dps = 1;

	int ostatok = dim - cols*rows*dps; 

	while( ostatok > 0 )
	{
		cols++;
		ostatok = dim - cols*rows*dps;
		if( ostatok > 0 )
		{
			rows++;
			ostatok = dim - cols*rows*dps;
		}
	}

	double pos_row = double(height)/rows;
	double pos_col = double(width)/cols;
	double pos_dps = double(depth)/dps;

	int i = -1;

	for ( int row = 1; row <= rows; row++ )
	{
		for( int col = 1; col <= cols; col++ )
		{
			for( int dep = 1; dep <= dps; dep++ )
			{
				i++;

				if ( i >= dim )
				{
					break;
				}

				x[i] = pos_col*( col - 0.5);
				y[i] =  pos_row*( row - 0.5);
				z[i] = pos_dps*( dep - 0.5);

				double r1 = rand()%100;
				double r = (r1/100);
				vx[i] = max_v*(2*r - 1);

				r1 = rand()%100;
				r = (r1/100);

				vy[i] = max_v*(2*r - 1);

				r1 = rand()%100;
				r = (r1/100);

				vz[i] = max_v*(2*r - 1);
			}
		}
	}

	double vxcum, vycum, vzcum;

	vxcum = vycum = vzcum = 0;

	for ( int i = 0; i < dim; i++ )
	{
		vxcum = vxcum + vx[i];
		vycum = vycum + vy[i];
		vzcum = vzcum + vz[i];
	}

	vxcum = vxcum / dim;
	vycum = vycum / dim;
	vzcum = vzcum / dim;

	for ( int i = 0; i < dim; i++ )
	{
		vx[i] = vx[i] - vxcum;
		vy[i] = vy[i] - vycum;
		vz[i] = vz[i] - vzcum;
	}

	os << dim << " " << width << " " << height << " " << depth << "\n";
	for ( int i = 0; i < dim; i++ )
	{
		os << x[i] << " " << y[i] << " " << z[i] << " " << vx[i] << " " << vy[i] << " " << vz[i] << "\n";
	}

	delete[] x;
	delete[] y;
	delete[] z;
	delete[] vx;
	delete[] vy;
	delete[] vz;

	os.close();
}